window.addEventListener('DOMContentLoaded', function (){

/* **************返回顶部，goback返回顶部模块*************/
    var searchAdv = document.querySelector('.search-adv');
    var nav = document.querySelector('.nav');
    var goback = document.querySelector('.goback');
    var html = document.documentElement;

    //下滑到主导航栏显示搜索框广告，goback返回顶部显示
    var navTop = nav.offsetTop;

    window.addEventListener('scroll',function(){//窗口滚动触发函数
        if (window.pageYOffset >= navTop){
            searchAdv.style.display = 'block';
            goback.style.display = 'block';
        } else {
            searchAdv.style.display = 'none';
            goback.style.display = 'none';
        }
    });
   
    //goback点击事件，点击或手指松开
    goback.addEventListener('click', returnTop);
    goback.addEventListener('touchend', returnTop);



    goback.addEventListener('click', returnTop);
    goback.addEventListener('touchend', returnTop);

    //返回顶部函数
    function returnTop(){    
        animal(window, 0);
        
    }
    
    //动画返回顶部动画函数
    //动画函数
    function animal(o, target, callback){
        
        clearInterval(o.timer1);

        o.timer1 = setInterval( function (){
            var top = o.pageYOffset;
            var step = (target - top) / 10; // 可以通过步长修改移动的速度快慢（缓动动画）
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (top == target){
                clearInterval(o.timer1);
                callback && callback();
            } else {
                o.scroll(0, top + step);
            }
        }, 15);
       
    }

/* **********返回顶部，goback返回顶部模块结束****************/
/* *********************倒计时模块**********************/
    var h = document.querySelector('#hours');
    var m = document.querySelector('#min');
    var s = document.querySelector('#sec');

    var now = +new Date();// 现在时间
    var endTime = +new Date(now + (1000 * 60 * 60 * 2));//2小时后

    function countDown(){
        var timer = setInterval(timing, 900);
        
        function timing(){
            now = +new Date();
            var res = (endTime - now) / 1000;
            var sec = parseInt(res % 60);
            sec = sec < 10 ? '0' + sec : sec;
            var min = parseInt(res / 60 % 60);
            min = min < 10 ? '0' + min : min;
            var hour = parseInt(res / 60 / 60 % 24);
            hour = hour < 10 ? '0' + hour : hour;
            h.innerHTML = hour;
            m.innerHTML = min;
            s.innerHTML = sec;

            
        }
    }
    countDown()
    






/* *********************倒计时模块结束**********************/
/* *********************轮播图模块**********************/   
    var lis = document.querySelectorAll('.focus-img li');
    var focusImg = document.querySelector('.focus-img');
    var banner = document.querySelector('.banner');
    var circle = document.querySelector('.circle');

    //克隆第一个和最后一个形成无缝滚动
    var firstLi = document.querySelector('.focus-img li:nth-of-type(1)').cloneNode(true);
    var lastLi = document.querySelector('.focus-img li:nth-last-child(1)').cloneNode(true);
    //将第一个和最后一个一个插入最后面和最前面形成无缝
    focusImg.appendChild(firstLi);// 插入第一个
    focusImg.insertBefore(lastLi, lis[0]);
    //插入小圆点
    
    function addingCircle(){
        for (var i = 0; i < lis.length; i++){
            var clis = document.createElement('li');
            circle.appendChild(clis);
        }
    }
    addingCircle();
    

    //自定义的一些变量
    var circleLis = circle.querySelectorAll('li');
    var w = 10;
    var index = 0;
    var startx = 0;
    var focustime = null;
    var distance = 0;
    var num = 0;
    var flag = false;

    function auto(){
        focusing();// 刚开始调用第一次，第一次成了1
        focustime = setInterval(focusing, 2000);
    }
    auto();

    //定时器函数
    function focusing(){
        /* console.log(index); */
        
        if (index >= lis.length + 1 || index <= -1){
           if (index >= lis.length + 1) {
                moving(index);
                
           } else if (index <= -1){
                moving(index);
           }
        } else {
            index++;
            moving(index);
            
        }
    }
    
    //封装动画函数
    function moving(index){
        var move = -(index * w);
        focusImg.style.transition = 'all .3s';
        focusImg.style.transform = 'translateX(' + move + 'rem)';
        
        //添加calss给小圆点，实现与img关联
        for (var i = 0; i < circleLis.length; i++){
            circleLis[i].classList.remove('circleWidth');
        }
        num = index;
        if (num == lis.length + 1){
            num = 1;
        } else if (num == 0){
            num = lis.length;
        }
        circleLis[num - 1].classList.toggle('circleWidth');
    }

    //完成动画后触发判断事件
    focusImg.addEventListener('transitionend', function(){
        if (index >= lis.length + 1) {
            index = 1;
        } else if (index <= 0){
            index = lis.length;
       }
    });
    // -1 1 2 3 4 5 6

    //手指触碰事件
    banner.addEventListener('touchstart', function (e){
        clearInterval(focustime);
        focustime = null;
        startx = e.targetTouches[0].pageX;
    });

    //手指滑动事件
    banner.addEventListener('touchmove', function (e){
        flag = true;
      if (flag){
        distance = (e.targetTouches[0].pageX - startx) / 100;
        var transformx = -(index) * w + distance;
        focusImg.style.transition = 'none';
        focusImg.style.transform = 'translateX(' + transformx + 'rem)';
      }
    });

    //手指离开事件
    banner.addEventListener('touchend', function (e){
        if (flag){
            
            if (Math.abs(distance) >= 100 / 100){
                if (distance > 0){
                    index--;
                    moving(index);
                } else {
                    index++;
                    moving(index);
                }
        } else {
            index = index;
            moving(index);
        }
        }
        focustime = setInterval(focusing, 2000);
    });
// -1 1 2 3 4 5 6

/* *********************轮播图模块**********************/
/* *********************苏宁头条轮播模块**********************/
    var focusList = document.querySelector('.focuslist');
    var ulFirst = document.querySelector('.focuslist ul');
    var listMove = document.querySelector('.list-move');
    var listlis = document.querySelectorAll('.focuslist li');
    

    //设置该模块变量
    var timer2 = null;
    var index2 = 0;
    var h = 1.226667;// 移动一个li的距离
    var mvoe = 0; // 移动距离
    var listlisLength = listlis.length;
    
    //无缝滚动最后一个添加第一个li
    ulFirst.appendChild(listlis[0].cloneNode(true));
    
    
    

    function MovingList(){
        moveUp();// 开始先执行一次
        timer2 = setInterval(moveUp, 2000);
        function moveUp(){
            mvoe = -index2 * h;
            listMove.style.transition = 'all .5s';
            listMove.style.transform = 'translateY(' + mvoe + 'rem)';
            index2++;
        }
    }
    MovingList();
    
    
    listMove.addEventListener('transitionend', function (){
        
        if (index2 == listlisLength + 1){
            index2 = 1;
            move = -index2 * h;
            listMove.style.transition = 'none';
            listMove.style.transform = 'translateY(' + mvoe + 'rem)';
            
        }
    });




    
// 1
// 2
// 3


/* *********************苏宁头条轮播模块结束**********************/
/* *********************苏宁顶部导航栏模块**********************/
    var searchContent = document.querySelector('.search-content');
    
    document.addEventListener('scroll', function (){
         if (window.pageYOffset <= 50){
            if (window.pageYOffset >= 1){
                console.log(1);
                searchContent.style.backgroundColor = 'rgba(255, 192, 1, ' + (1 - 0.02 * window.pageYOffset + 0.3) + ')';
            }
        }
    });
    









/* *********************苏宁顶部导航栏模块结束**********************/
});